package com.example.demo.api;

import com.example.demo.domain.Teacher;
import com.example.demo.service.TeacherService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/api/teacher")
public class TeacherApiController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private Gson gson;

    /**
     * 获得所有老师数据信息
     * @return
     */
    @GetMapping("/findAllTeachers")
    public List<Teacher> findAll(){
        List<Teacher> myAll = teacherService.findAll();
        return myAll;
    }

    /**
     * 获得指定id的老师数据信息
     * @param id  携带在请求体中
     * @return    指定id的老师数据信息
     */
    @GetMapping("/findById")
    public List<Teacher> findByTid(@RequestParam("tid") String id){
        Teacher myAll = teacherService.findByTid(id);
        // 创建一个ArrayList对象，并将myAll添加进去
        List<Teacher> teacherList = new ArrayList<>();
        teacherList.add(myAll);
        return teacherList;
    }

    /**
     * 找到最晚入校的老师信息
     * @return
     */
    @GetMapping("/findLatestTeacher")
    public List<Teacher> findLatestTeacher(){
        List<Teacher> myAll = teacherService.findLatestTeachers();
        return myAll;
    }

    /**
     * 删除指定id的老师信息
     * @param id
     */
    @DeleteMapping("")
    public void deleteById(@RequestParam("id") String id){
        Integer tid = Integer.parseInt(id);
        teacherService.deleteById(tid);
    }

    /**
     * 保存老师信息
     * @param teacher
     */
    @PostMapping("/save")
    public void saveTeacher(@ModelAttribute Teacher teacher){
            // 将接收到的教师信息保存到数据库中
            teacherService.saveTeacher(teacher);
    }
}
